package com.mxprojects.io
{
	import com.asual.swfaddress.SWFAddress;
	import com.asual.swfaddress.SWFAddressEvent;
	import com.mxprojects.gui.Resizer;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Admin;
	import com.mxprojects.views.ForgotPassword;
	import com.mxprojects.views.Forums;
	import com.mxprojects.views.Posts;
	import com.mxprojects.views.Profile;
	import com.mxprojects.views.Register;
	import com.mxprojects.views.Search;
	import com.mxprojects.views.Threads;
	
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class DeepLinking
	{		
		public static const CUSTOM_LINK:String = "customLink";
		
		private static const AUTHOR:String = "a";
		private static const FORUM:String = "f";
		private static const KEYWORD:String = "k";
		private static const LOCATION:String = "l";
		private static const PAGE:String = "p";
		private static const SEARCH_TYPE:String = "type";
		private static const THREAD:String = "t";
		private static const RESET_PASSWORD:String = "reset_password";
		private static const KEY:String = "key";
		private static const VERIFY_USER:String = "verifyuser";
		
		private static var customArray:Array = new Array();
		private static var linkParams:Array;		
		
		public static function setup():void {
			//this will initalize SWFAddress to do the good stuff kids go for
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, linkChange);
		}
		
		public static function setTitle(title:String = "", displayBoardName:Boolean = true):void {
			var browserTitle:String = Config.BOARD_NAME;
			if(title != ""){
				if(displayBoardName){
					browserTitle += ": " + title;
				}else{
					browserTitle = title;
				}
			}
			
			//make it the title
			SWFAddress.setTitle(browserTitle);
		}
		
		public static function set customFunction(functionToCall:Function):void {
			//this will set a function that will be called whenever a custom deep-link is found
			if(functionToCall != null){
				customArray[1] = functionToCall;
			}
		}
		
		public static function checkBackspace(event:KeyboardEvent):void {
			if(event.keyCode == Keyboard.BACKSPACE){
				//send the browser back
				trace("BACKSPACE DETECTED, MOVING BACK A PAGE");
				goBack();
			}
		}
		
		public static function getQueryString():String {
			return SWFAddress.getQueryString();
		}
		
		public static function goBack():Boolean {
			//allows us to go back in history, Doc Brown styles
			SWFAddress.back();
			
			//if the history isn't available let them know
			return SWFAddress.getHistory();
		}
		
		public static function goForward():void {
			SWFAddress.forward();
		}
		
		public static function createLinkArray(linkID:int = -1, pageNumber:int = 1, keywords:String = "", author:String = "", searchType:String = ""):Array {
			return new Array(linkID, pageNumber, keywords, author, searchType);
		}
		
		public static function setLink(location:String, linkArray:Array):void {
			//this will take anything and depending on where we are it'll handle stuff
			//[0]-LinkID [1]-Page Number
			//[2]-[5] for Search
			var linkID:int = linkArray[0];
			var pageNumber:int = linkArray[1];
			
			var keywords:String = linkArray[2];
			var author:String = linkArray[3];
			var searchType:String = linkArray[4];
			
			//decide which type of link to setup
			var linkValue:String = "/";
			switch(location){
				case Word.THREADS:
					linkValue = "?"+FORUM+"=" + linkID + "&"+PAGE+"=" + pageNumber;
				break;
				
				case Word.POSTS:
					linkValue = "?"+THREAD+"=" + linkID + "&"+PAGE+"=" + pageNumber;
				break;
				
				case Word.PROFILE:
				case Word.REGISTER:
				case Word.ADMIN:
					linkValue = "?"+LOCATION+"=" + location;
				break;
				
				case Word.SEARCH:
					//if searching we have keywords and authors, stored in extraStuff[0] and [1]
					linkValue = "?"+LOCATION+"=" + Word.SEARCH + "&"+PAGE+"=" + pageNumber;
					
					//check for keywords
					if(keywords == null){
						keywords = "";
					}
					linkValue += "&"+KEYWORD+"=" + keywords;
					
					//check for author
					if(author == null){
						author = "";
					}
					linkValue += "&"+AUTHOR+"=" + author;
					
					//check for topics
					if(searchType == null){
						searchType = "";
					}
					linkValue += "&"+SEARCH_TYPE+"=" + searchType;
				break;
				
				case CUSTOM_LINK:
					//if there is something in the link array, let's make sure to handle it
					linkValue += linkArray[0];
					customArray = linkArray;
				break;
			}
			
			SWFAddress.setValue(linkValue);
			
			//since we are setting a value that must mean the user is still active, so we need to reset the private message timer
			MXP3.header.refreshHeader();
		}
		
		public static function getValue():String {
			return SWFAddress.getValue().substring(1);
		}
		
		private static function linkChange(event:SWFAddressEvent):void {
			//handle when we've changed links			
			linkParams = SWFAddress.getParameterNames();
			
			//trace(SWFAddress.getQueryString());
			if(linkParams.length == 0 && getValue().length == 0){
				//if no params then we should load the home page
				Forums.load();
				setTitle();
			}else{
				//check the first paramter and decide what needs to be done
				var linkValue:String = SWFAddress.getParameter(linkParams[0]);
				var pageNumber:int = 1;
				var page:String = getParam(PAGE);
				if(page != "1" || page != ""){
					pageNumber = int(page);
				}
				
				trace("LINK CHANGE", linkParams);

				switch(linkParams[0]){
					case FORUM:
						Threads.load(int(linkValue), pageNumber);
					break;
					
					case THREAD:
						Posts.load(int(linkValue), pageNumber);
					break;
					
					case LOCATION:
						switch(linkValue){
							case Word.REGISTER:
								Register.load();
							break;
							
							case Word.PROFILE:
								Profile.load();
							break;
							
							case Word.ADMIN:
								Admin.load();
							break;
							
							case Word.SEARCH:
								if(getParam(KEYWORD) == "" && getParam(AUTHOR) == ""){
									Search.load();
								}else{
									Search.search(pageNumber, getParam(KEYWORD), getParam(AUTHOR),  getParam(SEARCH_TYPE));
								}
							break;
						}
					break;
					
					case VERIFY_USER:
						Forums.load();
						Register.verify(linkValue, getParam(KEY));
					break;
					
					case RESET_PASSWORD:
						Forums.load();
						ForgotPassword.reset(linkValue, getParam(KEY));
					break;
					
					default:
						//must be a custom URL, let them deal with it
						MXP3.currentLocation = Word.LOADING;
						Resizer.resizeContent();
						
						customArray[0] = getValue();
						
						//fire the function it needs
						if(customArray.length > 1){
							var customFunction:Function = customArray[1];
							customFunction.apply(null, customArray);
						}
					break;
				}
			}
		}
		
		private static function getParam(param:String):String {
			var index:int = linkParams.indexOf(param);
			if(index != -1){
				return SWFAddress.getParameter(linkParams[index]);
			}else{
				return "";
			}
		}

	}
}